from base import *
account = 1
while account <= 2:
    def all_swipe(height=int(driver.get_window_size()["height"]) - 50):
        height_avg = int(height / 3)
        currency_height = 200
        while height - currency_height > height_avg:
            driver.swipe(200, currency_height + height_avg, 200, currency_height, duration=500)
            currency_height += height_avg
            # print(driver.find_elements_by_xpath("//android.widget.Button"))
            # time.sleep(5)


    driver.wait_activity("com.dy.njyp.mvp.ui.activity.MainActivity", 20)
    '''
      webdirver 提供的方法。在设置时间内，默认每隔一段时间检测一次当前。页面元素是否存在，如果超过设置时间检测不到则抛出异常。
      driver ： WebDriver 的驱动程序(Ie, Firefox, Chrome 或远程)
      timeout ：最长超时时间，默认以秒为单位
      poll_frequency ：休眠时间的间隔（步长）时间，默认为 0.5 秒
      ignored_exceptions ：超时后的异常信息，默认情况下抛 NoSuchElementException 异常。
      WebDriverWai()一般和 until()或 until_not()方法配合使用，下面是 until()和 until_not()方法的说明。
      until(method, message=’’)：调用该方法提供的驱动程序作为一个参数，直到返回值不为 False。
      until_not(method, message=’’)：调用该方法提供的驱动程序作为一个参数，直到返回值为 False。
    
      element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId"))
      is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).until_not(lambda x: x.find_element_by_id("someId").is_displayed())
    '''
    driver.implicitly_wait(15)
    if isElement(By.ID, "com.hq.hardvoice:id/tv_app_update_cancel"):
        # 版本更新取消
        driver.find_element_by_id("com.hq.hardvoice:id/tv_app_update_cancel").click()

    if isElement(By.ID, "com.hq.hardvoice:id/likeview") is not True:
        print("未找到首页")
        driver.launch_app()
    driver.find_element_by_id("com.hq.hardvoice:id/likeview").click()

    counts = 0
    task_show = False
    if isElement(By.ID, "com.hq.hardvoice:id/iv_task_show"):
        # 首页已收起领取硬币
        task_show = True
        driver.find_element_by_id("com.hq.hardvoice:id/iv_task_show").click()
    else:
        print('观看作品')

    time0 = int(time.time())
    time1 = time0 + 10 * 60
    while True:
        driver.swipe(200, 900, 200, 210, duration=600)
        time.sleep(6)
        task_progress = None
        if isElement(By.ID, "com.hq.hardvoice:id/tv_get_progress") and (int(time.time()) > time1 or time0 + 10 > int(time.time())):
            task_progress = driver.find_element_by_id("com.hq.hardvoice:id/tv_get_progress")
        task_progress_btn = True if task_progress is not None and task_progress.text == '领硬币' else False
        if (int(time.time()) > time1 and task_progress_btn) or (task_show and task_progress_btn):
            break

    driver.find_element_by_id("com.hq.hardvoice:id/main_mine_tv").click()
    driver.find_element_by_id("com.hq.hardvoice:id/rl_task").click()
    driver.wait_activity("com.dy.njyp.mvp.ui.activity.mine.TaskCenterActivity", 20)
    # time.sleep(7)
    if isElement(By.XPATH,
                 "//*[contains(@text,'兑换中心')]/preceding-sibling::android.view.View/preceding-sibling::android.view.View"):
        receive_coin = driver.find_element_by_xpath(
            "//*[contains(@text,'兑换中心')]/preceding-sibling::android.view.View/preceding-sibling::android.view.View")
    else:
        print('硬币数显示不出!')

    receive_coins_num = int(receive_coin.text.replace(',', '')) if receive_coin.text else int(
        receive_coin.get_attribute('content-desc').replace(',', ''))
    del receive_coin
    sign_in = driver.find_element_by_class_name("android.widget.Button")
    if sign_in.text == '签到':
        counts += 1
        sign_in.click()
    # 真机模式高度设置1000，避免找不到按钮
    height = 1400 if inputVal not in ['3', '4'] else 1500
    print('高度200滑动到高度' + str(height))
    driver.swipe(200, height, 200, 100, duration=600)
    # time.sleep(5)

    contexts = driver.contexts
    print(contexts)
    num = 3
    btn_len = 0
    diff = 0
    while True:
        btn_len = driver.find_elements_by_xpath("//android.widget.Button")
        print('按钮数量%s' % str(len(btn_len)))
        if len(btn_len) > 0:
            break

        if height - diff <= 1000:
            diff += 1
        else:
            diff += 5
        driver.swipe(200, 100, 200, 1200, duration=500)
        '''driver.back()
        driver.find_element_by_id("com.hq.hardvoice:id/rl_task").click()
        driver.wait_activity("com.dy.njyp.mvp.ui.activity.mine.TaskCenterActivity", 20)
        time.sleep(5)
        '''
        print('高度200滑动到高度' + str(height - diff))
        driver.swipe(200, height - diff, 200, 200, duration=500)

    # 评论
    if False and isElement(By.XPATH, "//android.widget.Button[contains(@text,'去评论')]") is True:
        print('评论')
        driver.find_element_by_xpath("//android.widget.Button[contains(@text,'去评论')]").click()
        driver.wait_activity("com.dy.njyp.mvp.ui.activity.MainActivity", 15)
        while num > 0:
            driver.find_element_by_id("com.hq.hardvoice:id/comment").click()
            # driver.find_element_by_id("com.hq.hardvoice:id/messege_content").click()
            time.sleep(1)
            driver.find_element_by_id("com.hq.hardvoice:id/face_btn").click()
            driver.find_element_by_id("com.hq.hardvoice:id/face_image").click()
            driver.find_element_by_id("com.hq.hardvoice:id/tv_send").click()
            print('关闭评论按钮', isElement(By.ID, "com.hq.hardvoice:id/close"))
            driver.find_element_by_id("com.hq.hardvoice:id/close").click()
            time.sleep(1)
            driver.swipe(200, 1000, 200, 200, duration=500)
            num -= 1

        num = 3
        driver.find_element_by_id("com.hq.hardvoice:id/main_mine_tv").click()
        driver.find_element_by_id("com.hq.hardvoice:id/rl_task").click()
        time.sleep(5)
        driver.swipe(200, height, 200, 300, duration=500)
    else:
        print('未找到评论按钮')

    # 点赞
    if isElement(By.XPATH, "//android.widget.Button[contains(@text,'去点赞')]") is True:
        print('点赞')
        driver.find_element_by_xpath("//android.widget.Button[contains(@text,'去点赞')]").click()
        while num > 0:
            # time.sleep(0.5)
            if isElement(By.ID, "com.hq.hardvoice:id/like_iv") is True:
                driver.find_element_by_id("com.hq.hardvoice:id/like_iv").click()
            driver.swipe(200, 800, 200, 200, duration=500)
            num -= 1

        num = 3
        driver.find_element_by_id("com.hq.hardvoice:id/main_mine_tv").click()
        driver.find_element_by_id("com.hq.hardvoice:id/rl_task").click()
        while not isElement(By.XPATH,"//*[contains(@text,'兑换中心')]/preceding-sibling::android.view.View/preceding-sibling::android.view.View"):
            driver.back()
            driver.find_element_by_id("com.hq.hardvoice:id/rl_task").click()
        time.sleep(5)
        driver.swipe(200, height, 200, 300, duration=500)
    else:
        print('未找到点赞按钮')

    # 关注
    if isElement(By.XPATH, "//android.widget.Button[contains(@text,'去关注')]") is True:
        print('关注')
        driver.find_element_by_xpath("//android.widget.Button[contains(@text,'去关注')]").click()
        while num > 0:
            # time.sleep(0.5)
            if isElement(By.ID, "com.hq.hardvoice:id/follow") is True:
                driver.find_element_by_id("com.hq.hardvoice:id/follow").click()
                num -= 1
            driver.swipe(200, 800, 200, 200, duration=500)

        num = 3
        driver.find_element_by_id("com.hq.hardvoice:id/main_mine_tv").click()
        driver.find_element_by_id("com.hq.hardvoice:id/rl_task").click()
        while not isElement(By.XPATH,"//*[contains(@text,'兑换中心')]/preceding-sibling::android.view.View/preceding-sibling::android.view.View"):
            driver.back()
            driver.find_element_by_id("com.hq.hardvoice:id/rl_task").click()
        time.sleep(5)
        driver.swipe(200, height, 200, 300, duration=500)
    else:
        print('未找到关注按钮')

    # 看直播
    if isElement(By.XPATH,
                 "//android.view.View[@resource-id='container']/*[contains(@text,'观看直播')]/following-sibling::android.widget.Button[contains(@text,'去观看')]") is True:
        print('观看直播')
        driver.find_element_by_xpath(
            "//android.view.View[@resource-id='container']/*[contains(@text,'观看直播')]/following-sibling::android.widget.Button[contains(@text,'去观看')]").click()
        driver.wait_activity("com.dy.njyp.mvp.ui.activity.MainActivity", 20)
        while True:
            if isElement(By.XPATH, "//android.widget.TextView[contains(@text,'回看')]"):
                driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'回看')]").click()
                break
            else:
                driver.swipe(200, 900, 200, 210, duration=600)
        time1 = int(time.time()) + 10 * 60
        while int(time.time()) <= time1:
            time.sleep(3.34 * 60)
            driver.swipe(200, height, 200, 300, duration=500)
        if isElement(By.ID,"com.hq.hardvoice:id/iv_close"):
            driver.find_element_by_id("com.hq.hardvoice:id/iv_close").click()
        driver.find_element_by_id("com.hq.hardvoice:id/main_mine_tv").click()
        driver.find_element_by_id("com.hq.hardvoice:id/rl_task").click()
        while not isElement(By.XPATH,"//*[contains(@text,'兑换中心')]/preceding-sibling::android.view.View/preceding-sibling::android.view.View"):
            driver.back()
            driver.find_element_by_id("com.hq.hardvoice:id/rl_task").click()
        time.sleep(5)
        driver.swipe(200, height, 200, 300, duration=500)
    else:
        print('未找到观看直播按钮')

    # 领硬币
    receive_coins = driver.find_elements_by_class_name("android.widget.Button")
    print("按钮个数%s" % len(receive_coins))
    if len(receive_coins) > 0:
        for coin in receive_coins:
            if coin.text == '领硬币':
                counts += 1
                coin.click()
        time.sleep(0.3)
        if counts > 0:
            driver.swipe(200, 120, 200, height, duration=500)
            receive_coin = driver.find_element_by_xpath(
                "//*[contains(@text,'兑换中心')]/preceding-sibling::android.view.View/preceding-sibling::android.view.View")
            receive_coins_num_end = int(receive_coin.text) if receive_coin.text else int(
                receive_coin.get_attribute('content-desc').replace(',', ''))
            print("原有硬币：" + str(receive_coins_num) + "\n现有硬币：" + str(receive_coins_num_end))
            print('点击' + str(counts) + '个按钮领取硬币' + str(receive_coins_num_end - receive_coins_num), 'quit')

    driver.back()
    #取消关注
    driver.find_element_by_id("com.hq.hardvoice:id/ll_my_attention").click()
    attentions = 2
    for i in range(attentions):
        tv_follow_mpts = driver.find_elements_by_id("com.hq.hardvoice:id/tv_follow_mpt")
        for tv_follow_mpt in tv_follow_mpts:
            if tv_follow_mpt.text == '已关注':
                tv_follow_mpt.click()
        driver.swipe(200, height, 200, 300, duration=500)

    driver.back()
    if account >= 2:
        break
    tv_username = driver.find_element_by_id("com.hq.hardvoice:id/tv_username").text
    print('切换账号')
    driver.find_element_by_id("com.hq.hardvoice:id/iv_top_set").click()
    driver.find_element_by_id("com.hq.hardvoice:id/bt_set").click()
    driver.find_element_by_id("com.hq.hardvoice:id/loginout").click()
    time.sleep(5)
    if isElement(By.ID, "com.hq.hardvoice:id/tv_app_update_cancel"):
        print('版本更新取消')
        driver.find_element_by_id("com.hq.hardvoice:id/tv_app_update_cancel").click()

    if isElement(By.ID, "com.hq.hardvoice:id/likeview") is not True:
        print("未找到首页")
        driver.launch_app()
    driver.find_element_by_id("com.hq.hardvoice:id/main_mine_tv").click()
    driver.find_element_by_id("com.hq.hardvoice:id/checkbox_agreen").click()
    if tv_username == 'jf_70693385':
        print('微信快速登录')
        driver.find_element_by_id("com.hq.hardvoice:id/ll_wechat_login").click()
        if isElement(By.ID, "android:id/alertTitle"):
            if driver.find_element_by_id("android:id/alertTitle").text == '使用以下方式打开':
                driver.find_element_by_xpath("//android.widget.GridView/android.widget.LinearLayout[1]").click()
    elif isElement(By.ID,"com.hq.hardvoice:id/ll_fast_login"):
        print('手机号快速登录')
        driver.find_element_by_id("com.hq.hardvoice:id/ll_fast_login").click()
    elif isElement(By.ID,"com.hq.hardvoice:id/ll_mobile_login"):
        print('手机号登录')
        driver.find_element_by_id("com.hq.hardvoice:id/ll_mobile_login").click()
        if isElement(By.ID,"com.hq.hardvoice:id/tv_select_pwd_login"):
            driver.find_element_by_id("com.hq.hardvoice:id/tv_select_pwd_login").click()
        username = '18682411016'
        if tv_username == 'jf_70693385':
            username = '15700767384'
        driver.find_element_by_id("com.hq.hardvoice:id/et_validator_phone_email").send_keys(username)
        driver.find_element_by_id("com.hq.hardvoice:id/et_validator_password").send_keys('Qwer147852')
        driver.find_element_by_id("com.hq.hardvoice:id/btn_login").click()
    account += 1
driver.quit()
print('硬声app运行结束')